<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>货物管理页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">产品名称</label>
                            <div class="layui-input-inline">
                                <input id="keyword1" type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                         <div class="layui-inline">
                            <label class="layui-form-label">生产厂商</label>
                            <div class="layui-input-inline">
                                <input id="keyword2" type="text" name="factory" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="searchBtn" type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        	<button id="addBtn" type="button" class="layui-btn">添加产品</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-radius layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-radius layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        
        <!-- 定义了员工新增时弹出窗口时的模板 -->
        <script type="text/html" id="goodsForm">
			<form class="layui-form" action="" lay-filter="goodsForm">
				<div class="layui-form-item">
    				<div class="layui-input-inline">
      					<input type="hidden" id="id" name="id">
    				</div>
  				</div>

  				<div class="layui-form-item">
    				<label class="layui-form-label">产品名称</label>
    				<div class="layui-input-inline">
                        <label>
                            <input type="text" name="name" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
                        </label>
                    </div>
  				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label">生产厂家</label>
    				<div class="layui-input-inline">
                        <label>
                            <input type="text" name="factory" placeholder="请输入产品生产厂家" autocomplete="off" class="layui-input">
                        </label>
                    </div>
  				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label">产品型号</label>
    				<div class="layui-input-inline">
                        <label>
                            <input type="text" name="model" placeholder="请输入产品型号" autocomplete="off" class="layui-input">
                        </label>
                    </div>
  				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label">产品规格</label>
    				<div class="layui-input-inline">
                        <label>
                            <input type="text" name="specs" placeholder="请输入产品规格" autocomplete="off" class="layui-input">
                        </label>
                    </div>
  				</div>

				<div class="layui-form-item">
    				<div class="layui-input-block">
      					<button id="saveBtn" type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
  				</div>

			</form>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'laydate'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            laydate = layui.laydate;

        // 搜索按钮绑定事件
        $("#searchBtn").on("click", function(){
        	// table表格渲染数据
            table.render({
                elem: '#currentTableId',
                url: '/goods?action=goodsList', // 发送请求的地址
                where: {keyword1: $("#keyword1").val(), keyword2: $("#keyword2").val()},
                cols: [[
                    {field: 'id', title: 'ID', sort: true},
                    {field: 'name', title: '产品名称'},
                    {field: 'factory', title: '生产厂家'},
                    {field: 'model', title: '产品型号'},
                    {field: 'specs', title: '产品规格'},
                    {title: '操作', toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            }); //end table.render
        }).click(); //end $("#searchBtn")
        

       // 新增按钮绑定事件
       $("#addBtn").on("click", function(){
    	   layer.open({
    		   type: 1,
        	   title: "添加货物",
        	   area: ["420px", "400px"],
        	   content: $("#goodsForm").html()
        	});
          // 渲染表单
          form.render();
       });
        
        
    // 保存按钮绑定事件
  	$("body").on("click", "#saveBtn", function(){
           // 打包form表单中的所有数据
        let data = form.val("goodsForm");
           data.action = 'save';
           console.log(data);
           // 发送请求把数据推送后台
           $.post("/goods", data, function(){
           	// 请求成功后  1、关窗口，2、点搜索
           	layer.closeAll();
           	$("#searchBtn").click();
           }); //end $.post
       }); //end $("#saveBtn")


        // 表格操作工具条时间
        table.on('tool(currentTableFilter)', function (obj) {
            console.log(obj);
            if(obj.event === 'edit'){ // 点的是编辑
        		layer.open({
             		type: 1,
             		title: "编辑货物",
             		area: ["420px", "400px"],
             		content: $("#goodsForm").html()
             	});	
            	$.get("/goods?action=edit&id="+obj.data.id, function(){
                	form.val("goodsForm", {
                		"id": obj.data.id,
                		"name": obj.data.name,
                		"factory" : obj.data.factory,
                		"model" : obj.data.model,
                		"specs": obj.data.specs
                	}); //end form.val
                }); //end $.get
                // 渲染表单
                form.render();
            } //end if
			if(obj.event === 'delete'){ // 点的是删除
				layer.confirm("您确定要删除该货物吗？", function(){
					// 通知后台删除该员工（发请求）
					$.post("/goods", {action:'del', id:obj.data.id}, function(){
						// 关窗口
						layer.closeAll();
						// 重现渲染一遍table表格数据
						$("#searchBtn").click();
					}); //end $.post
				}); //end layer.confirm
				
          	}
        });

    });
</script>

</body>
</html>